package com.offics.loan.infrastructure.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offics.loan.domain.entity.UserInfo;
import com.offics.loan.domain.repository.UserInfoRepository;
import com.offics.loan.infrastructure.persistence.mapper.UserInfoMapper;
import org.springframework.stereotype.Repository;

import java.util.Optional;

/**
 * 用户信息仓储实现类
 */
@Repository
public class UserInfoRepositoryImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoRepository {

    @Override
    public Optional<UserInfo> findById(Long id) {
        return Optional.ofNullable(getById(id));
    }

    @Override
    public Optional<UserInfo> findByUserId(Long userId) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUserId, userId);
        return Optional.ofNullable(getOne(wrapper));
    }

    @Override
    public boolean save(UserInfo userInfo) {
        return super.save(userInfo);
    }

    @Override
    public UserInfo saveAndReturn(UserInfo userInfo) {
        if (save(userInfo)) {
            return userInfo;
        }
        return null;
    }

    @Override
    public boolean update(UserInfo userInfo) {
        return updateById(userInfo);
    }

    @Override
    public boolean deleteById(Long id) {
        return removeById(id);
    }

    @Override
    public boolean deleteByUserId(Long userId) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUserId, userId);
        return remove(wrapper);
    }
}
